let api = require("../../mars/services/api.js");
let utils = require("../../utils/util.js");

let that;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      avatar_url: "",
      userName: "",
      id: ""
    },
    group: {
      group_name: "",
      description: "",
    },
    customerCounts: 0,
    punchedUsers: [],
    unpunchedUsers: [],

    punchData: [],
    allEvents: [],
    activityIndex: 0,
    teammatesPunchedList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this;
    let userId = wx.getStorageSync('user').id;
    that.setData({
      groupId: options.groupId
    });
    that.getGroupById(options.groupId);
    that.getUnpunchedUsersInGroup(options.groupId, '全部');
    that.getAllAvailbleEvents();
    that.teammatesPunchedList(options.groupId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  navigateBack() {
    wx.navigateBack({
      delta: 1,
    })
  },
  getGroupById(groupId) {
    api.WxRequest(api.getGroupId, {
      groupId: groupId
    }, function(res) {
      if (!res.status) {
        that.setData({
          group: res.data
        })
      }
    })
  },
  getUnpunchedUsersInGroup(groupId, eventTag) {
    if (eventTag == '全部') eventTag = '';
    api.WxRequest(api.getUsersUnpunched, {
      groupId: groupId,
      eventTag: eventTag
    }, function(res) {
      if (res.data) {
        that.setData({
          punchedUsers: res.data.punchedInGroup,
          unpunchedUsers: res.data.unpunchedInGroup
        })
      }
    })
  },
  bindActivityTagChange(e) {
    this.setData({
      activityIndex: e.detail.value
    });
    that.getUnpunchedUsersInGroup(that.data.groupId, that.data.allEvents[e.detail.value]);
  },
  getAllAvailbleEvents() {
    api.WxRequest(api.getAllAvailbleEvents, {}, (res) => {
      if (res.status == 0) {
        that.setData({
          allEvents: ['全部'].concat(res.data.map(item => {
            return item.event_tag
          }))
        })
      }
    })
  },
  nevitageToCommunityCalender(e) {
    let userId = e.target.dataset.userid;
    let userName = e.target.dataset.username;
    let avatarUrl = e.target.dataset.avatarurl;
    let opts = `?userId=${userId}&userName=${userName}&avatarUrl=${avatarUrl}`;
    wx.navigateTo({
      url: '/pages/communityCalender/communityCalender' + opts
    });
  },
  teammatesPunchedList(groupId) {
    api.WxRequest(api.getTeammatesPunchedList, {
      groupId: groupId
    }, (res) => {
      that.setData({
        teammatesPunchedList:res.data || []
      })
    });
  }
})